package web.controller.base;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
 //SSM
//struts1
@WebServlet(name ="DispacherServlet",urlPatterns = {"*.action","*.do"})
public class DispacherServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        System.out.println("执行初始化的方法: 项目启动时,的一些初始化的通用数据 获取数据库连接 全局的参数配置 等等");//application / redis
    }

     @Override
     public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

         System.out.println("进入了指定义MVC controller(核心控制器),由DispacherServlet.service 分发  所用的用户请求");
         request.setCharacterEncoding("UTF-8"); // 设置请求编码方式
         response.setContentType("text/html; charset=UTF-8"); // 设置响应内容格式和编码方式

         PrintWriter out =response.getWriter();
         String opr = request.getParameter("opr");

         try {
             String contextPath = request.getContextPath();//   MyMVC
             String uri =request.getRequestURI();           //  MyMVC/user?opr=xxxx&userName=xxx&userId=xxx

             //根据上下文路径得到uri ,解析 --> UserController
             //调用对应的newInstance();
             //该转发转发, 该重定向就重定向, out就out
             // String uri = "/MyMVC/book.do";
            
			String url = uri.substring(uri.lastIndexOf("/")+1,uri.lastIndexOf(".")); //login
            

			url = url.substring(0,1).toUpperCase().concat(url.substring(1)).concat("Controller");
			 
             url = "web.controller.".concat(url);
             System.out.println(url);


             SuperController controller = (SuperController) Class.forName(url).newInstance();

             String targetUrl =controller.execute(request,response);

             String perfix = targetUrl.substring(0,targetUrl.indexOf(":"));
             targetUrl =targetUrl.substring(targetUrl.lastIndexOf(":")+1);
             //该转发转发, 该重定向就重定向, out就out
             if (perfix.startsWith("out")){
                 out.print(targetUrl);
                 return;
             }else if(perfix.startsWith("redirect")){
                 response.sendRedirect(targetUrl);
             }else{
                 request.getRequestDispatcher(targetUrl).forward(request,response);
             }

         } catch (InstantiationException e) {
             e.printStackTrace();
         } catch (IllegalAccessException e) {
             e.printStackTrace();
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         } finally {
             out.flush();
             out.close();
         }
     }
    @Override
    public void destroy() {
        System.out.println("************销毁资源(数据库的资源  缓存的资源)***********");
        super.destroy();
    }
}
